package com.rabbit.config;

import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
import com.rabbit.constant.SecurityConstant;
import com.rabbit.util.StpCustomerUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <h1>拦截器相关配置</h1>
 *
 * @author zjw
 * @date 2021/6/29 15:29
 */
@Configuration
public class DefaultWebMvcConfig extends AbstractWebMvcConfig implements WebMvcConfigurer {

    @Override
    protected void addCustomInterceptors(InterceptorRegistry registry) {
        // Sa路由拦截器
        registry.addInterceptor(new SaRouteInterceptor((req, res, handler) -> {
            // OPTIONS跳过
            SaRouter.match(SaHttpMethod.OPTIONS).back();
            // 认证
            SaRouter.match("/**").notMatch("/login", "/druid/*", "/doc.html", "/webjars/**", "/swagger-resources/**").check(r -> StpCustomerUtil.checkLogin());
            SaRouter.match("/index/**", r -> StpCustomerUtil.checkRole(SecurityConstant.APP_USER));
        })).addPathPatterns("/**");
    }
}
